<?php

// 参考：http://www.php.net/manual/zh/pdo.connections.php
// https://segmentfault.com/a/1190000004106175

include_once 'db_config.php';
try {
// array(PDO::ATTR_PERSISTENT => true) 开启持久化连接
    $dbh = new PDO('mysql:host=localhost;dbname=wx', $db['user'], $db['pwd'], array(PDO::ATTR_PERSISTENT => true));

    if (!empty($_POST["current_date"])) {
        // 获取指定日期的净增用户总量
        $current_day = $_POST["current_date"];
    } else {
        $current_day = date('Y-m-d', strtotime('-1 days'));
    }
//    echo $current_day;
//    $sql = 'SELECT share_scene as scene,share_count as count,share_user as user FROM `usershare` where ref_date="' . $current_day . '" ';
    $sql = 'SELECT share_scene,share_count,share_user FROM `usershare` where ref_date="' . $current_day . '" ';
    $arr = array();
    foreach ($dbh->query($sql) as $row) {
//        var_dump($row);
        // 把日期和用户数单独组成列 | 变量追加到数组中
        $arr['date'][] = $current_day;
        switch ($row['share_scene']) {
            case 1:
                $arr['scene'][] = '好友转发';
                break;
            case 2:
                $arr['scene'][] = '朋友圈';
                break;
            case 3:
                $arr['scene'][] = '腾讯微博';
                break;
            case 5:
                $arr['scene'][] = '其他';
                break;
        }
        $arr['count'][] = $row['share_count'];
        $arr['user'][] = $row['share_user'];
    }

    // 把数据转换成json格式
    $data = json_encode($arr);
    echo $data;

    // 现在运行完成，在此关闭连接
    $dbh = null;
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}
?>